package cn.edu.ctbu.studentmanage.api;

import cn.edu.ctbu.studentmanage.core.PageUtils;
import cn.edu.ctbu.studentmanage.core.RUtil;
import cn.edu.ctbu.studentmanage.domain.Teacher;
import cn.edu.ctbu.studentmanage.service.TeacherService;
import cn.edu.ctbu.studentmanage.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class TeacherApiController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 读取教师信息./api/teacherindex
     * @return
     */
    @GetMapping("/teacherindex")
    public R findAll(){
        return RUtil.success(teacherService.findAll());
    }

    /**
     * 根据id查找教师信息./api/getteacher/tid
     * @param tid
     * @return
     */
    @GetMapping("/getteacher/{tid}")
    public R get(@PathVariable Integer tid) {
        Teacher teacher = teacherService.getByTid(tid);

        return RUtil.success(teacher);
    }

    /**
     * 新增教师./api/insertteacher
     * @param teacher
     * @param bindingResult
     */
    @PostMapping("/insertteacher")
    public R insert(@Valid Teacher teacher, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            String errStr = bindingResult.getFieldError().getDefaultMessage();
            log.error(errStr);
        }
        teacherService.Insert(teacher);

        log.info(teacher.toString());
        return RUtil.success(null);
    }

    /**
     * 更新教师信息./api/updateteacher
     * @param teacher
     */
    @PutMapping ("/updateteacher")
    public R update(Teacher teacher){

        teacherService.update(teacher);

        log.info(teacher.toString());
        return RUtil.success(null);
    }

    /**
     * 根据id删除某一教师./api/deleteteacher/tid
     * @param tid
     */
    @DeleteMapping ("/deleteteacher/{tid}")
    public R delete(@PathVariable Integer tid){

        teacherService.delete(tid);

//        log.info(Teacher.toString());
        return RUtil.success(null);
    }

    /**
     *
     * 分页读取全部内容./api/getbypageteacher
     * @return
     */
    @GetMapping("/getbypageteacher")
    public R findByPage(@RequestParam(value = "page",defaultValue = "0")Integer page,
                                @RequestParam(value = "size",defaultValue = "10")Integer size,
                                @RequestParam(value = "tname",defaultValue = "")String tname,
                                @RequestParam(value = "tsex",defaultValue = "")String tsex,
                                @RequestParam(value = "tdept",defaultValue = "")String tdept
    ){

        Page<Teacher> teacherPage ;
        if(StringUtils.isEmpty(tname) && StringUtils.isEmpty(tdept)&& StringUtils.isEmpty(tsex)
        ){
            Pageable pageable = PageRequest.of(page,size, Sort.by("tid"));
            teacherPage =teacherService.findAll(pageable);
        }else {
            Teacher teacher = new Teacher();
            teacher.setTname(tname);
            teacher.setTsex(tsex);
            teacher.setTdept(tdept);

            Pageable pageable = PageRequest.of(page,size,Sort.by("tid"));
            ExampleMatcher matcher =ExampleMatcher.matching()
                    .withMatcher("tname",ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("tsex",ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("tdept",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Teacher> example = Example.of(teacher,matcher);
//        Example<Teacher> example = Example.of(Teacher);
            teacherPage = teacherService.findAll(example,pageable);
        }

        PageUtils pageUtils = new PageUtils(teacherPage.getContent(),teacherPage.getTotalElements());
        return RUtil.success(pageUtils);
    }
}